package com.hc.programming.interview;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 精灵云二面题
 *
 * @author huangchao E-mail:fengquan8866@163.com
 * @version 创建时间：2024/7/25 11:14
 */
public class 线程池core为0执行结果 {
    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(0, 4, 60, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(8), new ThreadPoolExecutor.AbortPolicy());
        // 假如8改成9，会有2个线程并行执行
        for (int i = 0; i < 8; i++) {
            executor.execute((createTask(1000, i)));
        }
        executor.shutdown();
    }

    private static Runnable createTask(long time, int n) {
        return () -> {
            try {
                Thread.sleep(time);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(n + "-" + Thread.currentThread().getId());
        };
    }
}
